package org.hxyjs.dao.impl;

import org.hxyjs.dao.ParentsDao;
import org.hxyjs.entity.Parents;
import org.hxyjs.entity.Parents;
import org.hxyjs.entity.Parents;
import org.hxyjs.entity.ParentsExample;
import org.hxyjs.mapper.ParentsMapper;
import org.hxyjs.page.PageInfo;
import org.hxyjs.utils.GenNumTools;
import org.hxyjs.vo.ParentsVo;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Repository("parentsDao")//一般实现类不会直接生成默认对象名 而是使用 接口的默认对象名
public class ParentsDaoImpl implements ParentsDao {
    @Resource //autowired 会装配能够直接扫描到的对象  resource spring容器运行的时候才找到需要的对象  注入
    private ParentsMapper parentsMapper;
    public List<Parents>  getAllParentss() throws Exception{
        return parentsMapper.selectByExample(null);
    }

    @Override
    public Integer deleteParentsByPno(String pno)  throws Exception{
        return parentsMapper.deleteByPrimaryKey(pno);
    }

    @Override
    public Integer updateParentsByPno(Parents parents)  throws Exception{
        return parentsMapper.updateByPrimaryKey(parents);
    }

    @Override
    public Integer addParents(Parents parents)  throws Exception{
        parents.setPno(initId());
        return parentsMapper.insertSelective(parents);
    }
    public String initId(){
        String maxPno = parentsMapper.getMaxPno();
        return GenNumTools.initId("",3,maxPno);
    }

    @Override
    public List<Parents> getParentsByPage(PageInfo pageInfo )  throws Exception{
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("currPage",pageInfo.getCurrPage());
        map.put("pageSize",pageInfo.getPageSize());
        int itemCount = (int)parentsMapper.countByExample(null);
        pageInfo.setItemCount(itemCount);
        return parentsMapper.getParentsByPage(map);
    }

    @Override
    public Parents selectParentsWithChildren(String pno){
        return parentsMapper.selectParentsWithChildren(pno);
    }

    @Override
    public Parents getParentsWithOrdrees(String pno) throws Exception {
        return parentsMapper.getParentsWithOrdrees(pno);
    }

    @Override
    public Parents getAccountByAccountName(String pname) {
            return parentsMapper.getAccountByAccountName(pname);
    }

    /**
     *
     * @param pname
     * @return
     */
    public List<Parents> getParentsByExample(String pname) {
        ParentsExample parentsExample =new ParentsExample();
        parentsExample.createCriteria().andPnameEqualTo(pname);
        return parentsMapper.selectByExample(parentsExample);
    }

    @Override
    public Integer updateParents(Parents parents) { return parentsMapper.updateByPrimaryKey(parents); }

    public Integer updateByPrimaryKeySelective(Parents parents) { return parentsMapper.updateByPrimaryKeySelective(parents); }


    @Override
    public Integer updateParentss(Parents parents) { return parentsMapper.updateByPrimaryKeys(parents); }

}
